    #include"block.h"

// ------------------------------- block basic function -----------------------------------

block::block()
{
	block_position = -1;
	last_use = CTime::GetCurrentTime();
	valid = false;
	modified = false;
	number=0;
	record_size=0;
	((int*)space)[0]=number;
}

block::block(CString f,int b,int r)
{
	file_name=f;
	block_position = b;
	last_use = CTime::GetCurrentTime();
	valid = false;
	modified = false;
	number=0;
	record_size=r;
	((int*)space)[0]=number;
}

void block::set(CString f,int b,int r)
{
	file_name=f;
	block_position = b;
	last_use = CTime::GetCurrentTime();
	valid = false;
	modified = false;
	number=0;
	record_size=r;
	((int*)space)[0]=number;
}

block::~block()
{
}

void block::setSize(int s)
{
	record_size=s;
}

void block::DBwrite()
{
	if(valid&&modified)
	{	
		CFile f;
		f.Open(file_name,CFile::modeCreate | CFile::modeNoTruncate| CFile::modeReadWrite);
		f.Seek(BLOCKSIZE*block_position, CFile::begin); 
		f.Write((void*)space,BLOCKSIZE);
		f.Close();
	}
}

void block::DBread(CString fn, int position)
{
	file_name=fn;
	block_position=position;
	CFile f;
	f.Open(file_name, CFile::modeNoTruncate| CFile::modeReadWrite);
	f.Seek(BLOCKSIZE*block_position, CFile::begin); 
	f.Read(space,BLOCKSIZE);
	f.Close();
	number=((int*)space)[0];
	last_use=CTime::GetCurrentTime();
	valid=true;
}

bool block::inBuffer(CString f, int position)
{
	return (file_name==f)&&(block_position==position);
}

